<template>
  <div>
    <h2>当前求和为：{{ $store.state.countAbout.sum }}</h2>
    <h4>放大10倍为：{{ $store.getters['countAbout/bigSum'] }}</h4>
    <h4>
      我在{{ $store.state.countAbout.school }}学{{
        $store.state.countAbout.subject
      }}
    </h4>
    <select name="" id="" v-model.number="n">
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
    </select>
    <button @click="increment">+</button>
    <button @click="decrement">-</button>
    <button @click="incrementOdd">当前求和为奇数再加</button>
    <button @click="incrementWait">等一等再加</button><br />
    <h2>map映射store</h2>
    <h4>放大10倍为：{{ bigSum }}</h4>
    <h4>我在{{ school }}学{{ subject }}</h4>
    <button @click="ADD(n)">+</button>
    <button @click="SUB(n)">-</button>
    <button @click="addOdd(n)">当前求和为奇数再加</button>
    <button @click="addWait(n)">等一等再加</button><br />
    <h4>下方Person组件的总人数是{{ personList.length }}</h4>
  </div>
</template>

<script>
import { mapState, mapGetters, mapActions, mapMutations } from 'vuex'

export default {
  name: 'Count',
  data() {
    return {
      n: 1,
    }
  },
  created() {},
  methods: {
    // 自己写方法调用store
    increment() {
      // 无需action可直接commit给Mutation
      this.$store.commit('countAbout/ADD', this.n)
    },
    decrement() {
      this.$store.commit('countAbout/SUB', this.n)
    },
    incrementOdd() {
      this.$store.dispatch('countAbout/addOdd', this.n)
    },
    incrementWait() {
      this.$store.dispatch('countAbout/addWait', this.n)
    },

    // //靠mapActions生成：incrementOdd、incrementWait（对象形式）
    // ...mapActions({ incrementOdd: 'jiaOdd', incrementWait: 'jiaWait' }),
    //靠mapActions生成：incrementOdd、incrementWait（数组形式）
    ...mapActions('countAbout', ['addOdd', 'addWait']),

    // //靠mapActions生成：increment、decrement（对象形式）
    // ...mapMutations({increment:'JIA',decrement:'JIAN'}),
    //靠mapMutations生成：JIA、JIAN（对象形式）
    ...mapMutations('countAbout', ['ADD', 'SUB']),
  },
  computed: {
    // //借助mapState生成计算属性：sum、school、subject（对象写法）
    // ...mapState({sum:'sum', school:'school', subject:'subject'}),
    //借助mapState生成计算属性：sum、school、subject（数组写法）
    ...mapState('countAbout', ['sum', 'school', 'subject']),
    ...mapState('personAbout', ['personList']),

    // //借助mapGetters生成计算属性：bigSum（对象写法）
    // ...mapGetters({ bigSum: 'bigSum' }),
    //借助mapGetters生成计算属性：bigSum（数组写法）
    ...mapGetters('countAbout', ['bigSum']),
  },
  mounted() {
    console.log('@@@@@', this.$store)
  },
}
</script>

<style scoped>
button {
  margin-left: 5px;
}
</style>
